package com.xty.itgroup.tech05tytex.Logic;


import com.alibaba.fastjson.JSONObject;
import com.xty.itgroup.tech05tytex.Databean.Bean01YarnNew;
import com.xty.itgroup.tech05tytex.Databean.Bean01YarnTag;
import com.xty.itgroup.tech05tytex.Databean.Bean02ClothBom;
import com.xty.itgroup.tech05tytex.Databean.Bean02ClothTags;
import com.xty.itgroup.tech05tytex.Databean.Bean02YarnTags;
import com.xty.itgroup.tech05tytex.Databean.BeanException;
import com.xty.itgroup.tech05tytex.Mapper.Impl.Impl01Base;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Logic01Base {
    @Autowired
    private Impl01Base base;

    @Transactional
    public void addYarns(List<Map<String, Object>> list) {
        base.deleteYarns();
        base.addYarns(list);
    }

    @Transactional
    public void addYarnsNew(List<Map<String, Object>> list) {
        base.deleteYarnsNew();
        base.addYarnsNew(list);
    }

    @Transactional
    public void addGreyBomDetail(List<Map<String, Object>> list) {
        //获取坯布工艺-子表
        base.deleteGreyBom();
        base.addGreyBom(list);

    }
    @Transactional
    public  void addGreyBomMain(List<Map<String, Object>> list) {
        //获取-主表
        base.deleteGreyBomMain();
        base.addGreyBomMain(list);
    }

    //添加纱线加工属性
    public void addYarnTag(String tag_name, int parent_id, double tag_unit, int type) {
        Bean01YarnTag yarntag = new Bean01YarnTag().setTag_name(tag_name)
                .setTag_unit(tag_unit)
                .setParent_id(parent_id)
                .setTag_type(type);
        //添加
        base.addTag(yarntag);
    }

    //修改加工属性
    @Transactional
    public void updateYarnTag(Bean01YarnTag tag) {
        //删除原有的标签
        base.updateTag(tag.getId());
        Bean01YarnTag t = new Bean01YarnTag()
                .setTag_unit(tag.getTag_unit())
                .setParent_id(tag.getParent_id())
                .setTag_name(tag.getTag_name())
                .setTag_type(tag.getTag_type());

        //添加新的标签
        base.addTag(t);
        int new_id = t.getId();
        if (tag.getTag_type()==1){
            base.updateTagByParentId(tag.getParent_id(),new_id);
        }
        //查找绑定关系-
        List<Bean02YarnTags> yts = base.listYarnsByTag(tag.getId() + "", tag.getId() + "", tag.getId() + "",
                tag.getId() + "");
        //删除绑定关系
        String ids = "";
        List<Bean02YarnTags> new_yts = new ArrayList<>();
        for (Bean02YarnTags yt : yts) {
            ids = ids + yt.getYarn_id() + ",";
            String[] s = yt.getTag_ids().split(",");
            for (int i = 0; i < s.length; i++) {
                if ((tag.getId() + "").equals(s[i])) s[i] = new_id + "";
            }
            yt.setTag_ids(StringUtils.join(s));
            new_yts.add(yt);
        }
        if (ids.length() != 0) ids = ids.substring(0, ids.length() - 1);
        //删除
        base.updateYarnTags(ids);
        //新增
        if (new_yts != null && new_yts.size() != 0) {
            base.addYarnTags(new_yts);
            //计算价格
            base.calcualtUnit();
            //计算布价格

        }


    }

    public Map<String, List<Bean01YarnTag>> listTags() {
        List<Bean01YarnTag> tags = base.listTags();
//        Map<String, List<Bean01YarnTag>> glist = tags.stream().collect(Collectors.groupingBy
//        (Bean01YarnTag::getTag_type));
        Map<String, List<Bean01YarnTag>> map = new HashMap<>();
        for (Bean01YarnTag t : tags) {
            List<Bean01YarnTag> l = null;
            if (!map.containsKey("tag" + t.getTag_type())) {
                l = new ArrayList<>();
            } else {
                l = map.get("tag" + t.getTag_type());
            }
            l.add(t);
            map.put("tag" + t.getTag_type(), l);
        }
        return map;
    }

    public List<Bean01YarnTag> listTreeTags() {
        List<Bean01YarnTag> tags = base.listTags();
        List<Bean01YarnTag> res = getChild(tags);
        return res;
    }
    //获取根节点
    public List<Bean01YarnTag> getChild(List<Bean01YarnTag> as){
        List<Bean01YarnTag> result=new ArrayList<>();
        for(Bean01YarnTag l:as){
            if (l.getParent_id()==0){
                result.add(getTree(l,as));
            }
        }
        return result;
    }
    //开枝散叶
    public Bean01YarnTag getTree(Bean01YarnTag auth,List<Bean01YarnTag> ll){
        List<Bean01YarnTag> childtree=new ArrayList<>();
        for(Bean01YarnTag l:ll){
            if (auth.getId()==l.getParent_id()){
                //开枝散叶的精髓
                childtree.add(getTree(l,ll));
            }
        }
        auth.setChild_tags(childtree);
        return auth;
    }
    //绑定纱线-加工属性
    @Transactional
    public void addYarnTags(String tag_ids, String yarn_ids, double yarn_price, int flag) {
        //yarn_ids 查询重复
        List<Bean02YarnTags> list = base.listYarnTagsByIds(yarn_ids);
        //if重复 已有纱线标签，是否覆盖原有标签
        if (list != null && list.size() != 0 && flag == 0) {
            throw new BeanException(1, "部分纱线已有绑定标签，是否重新绑定？");
        }
        if (flag == 1) base.updateYarnTags(yarn_ids);
        String[] ids = yarn_ids.split(",");
        List<Bean02YarnTags> tags = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            Bean02YarnTags yarnTags = new Bean02YarnTags().setYarn_id(Integer.parseInt(ids[i]))
                    .setYarn_price(yarn_price)
                    .setTag_ids(tag_ids);
            tags.add(yarnTags);
        }
        //添加
        base.addYarnTags(tags);
    }

    public void update1YarnTag(int id, String tag_ids, double yarn_unit) {
        Bean02YarnTags yt = base.getYarnTag(id);
        if (yt != null) {
            yt.setTag_ids(tag_ids).setYarn_price(yarn_unit);
            List<Bean02YarnTags> l = new ArrayList<>();
            l.add(yt);
            base.addYarnTags(l);
        } else {
            throw new BeanException(1, "未查到相关价格");
        }

    }

    public List<Bean01YarnNew> listYarns(String yarn_name, int type) {
        yarn_name = yarn_name.replace(" ", "%");
        return base.listYarns(yarn_name, type);
    }

    //添加织造属性，价格
    public void addWeaveTag(List<Bean02ClothTags> tags) {
        base.addClothTags(tags);
    }
    public void add1WeaveTag(Bean02ClothTags tags) {
        Bean02ClothTags tag= base.getClothByName(tags.getWeave_name());
        if (tag!=null) base.deleteClothTags(tag.getId());
        base.addClothTag(tags);
    }
    //模糊查询品名
    public List<Bean02ClothBom> listProduct(String product_name) {
        return base.listProduct(product_name);
    }

    //查询价格 根据品名查询
    public JSONObject searchClothUnit(String product_name) {
        int flag = 0;
        //1-根据品名查是否已有单价
        Bean02ClothBom cloth = base.getProduct(product_name);
        List<Map<String,Object>> tag = base.listClothWithUnit1(product_name,-1);
        if (tag.size()==1&&tag.get(0).get("id")==null) {
            tag = new ArrayList<>();
        }else{
            flag = 1;
        }

        //有 flag=1
        //2-如果flag=1 直接取到对应生产属性tag_id
        //  如果flag=0
        //3-获取坯布工艺并获取单价
        List<Map<String, Object>> boms = base.listBomDetail(product_name);
        Map<String, List<Bean01YarnTag>> maps = listTags();
        JSONObject res = new JSONObject();
        res.put("cloth_tags", flag == 1 ? tag : "");
        res.put("boms", boms);
        res.put("param", maps);
        res.put("cloth", cloth);
        return res;
    }

    //获取纱线列表带价格
    public List<Map<String, Object>> listYarnsWithUnit(String yarn_name) {
        return base.listYarnsWithUnit(yarn_name);
    }

    //获取布的列表带价格
    public List<Map<String, Object>> listClothWithUnit(String product_name , int flag) {
        List<Map<String, Object>> list=  base.listClothWithUnit(product_name,flag);
        if (list.size()==1&&list.get(0).get("id")==null) return new ArrayList<>();
        return list;
    }
    //获取布的列表带价格（精确查询）
    public List<Map<String, Object>> listClothWithUnit1(String product_name,int flag) {
        List<Map<String, Object>> list=  base.listClothWithUnit1(product_name,flag);
        if (list.size()==1&&list.get(0).get("id")==null) return new ArrayList<>();
        return list;
    }
}
